package com.xiaoshuidi.cloud.module.system.controller.admin.oss;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.operatelog.core.annotations.OperateLog;
import com.xiaoshuidi.cloud.module.system.dal.dataobject.oos.SystemImageDO;
import com.xiaoshuidi.cloud.module.system.dal.mysql.oos.SystemImageMapper;
import com.xiaoshuidi.cloud.module.system.service.oos.OosService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.util.Map;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name =  "管理后台 - oss")
@RestController
@RequestMapping("/system/oss")
@Validated
@Slf4j
public class OssController {

    @Resource
    private OosService oosService;

    @Resource
    private SystemImageMapper imageMapper;

    @PostMapping("/upload")
    @PermitAll
    @Operation(summary = "oss上传文件")
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public CommonResult<Map<String, Object>> upload(@RequestPart("file") MultipartFile file) {
        return success(oosService.uploadFile(file,file.getOriginalFilename()));
    }

    @PostMapping("/add")
    @PermitAll
    @Operation(summary = "oss本地库表url增加功能")
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public CommonResult<Integer> addURL(@RequestBody SystemImageDO systemImageDO) {
        return success(imageMapper.insert(systemImageDO));
    }

    @DeleteMapping("/delete/{id}")
    @PermitAll
    @Operation(summary = "oss本地库表url删除功能")
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public CommonResult<Integer> deleteURL(@PathVariable(name = "id") Integer id) {
        return success(imageMapper.deleteById(id));
    }

    @PostMapping("/update")
    @PermitAll
    @Operation(summary = "oss本地库表url修改功能")
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public CommonResult<Integer> updateURL(@RequestBody SystemImageDO systemImageDO) {
        return success(imageMapper.updateById(systemImageDO));
    }

    @GetMapping("/get/{id}")
    @PermitAll
    @Operation(summary = "oss本地库表url根据id查询功能")
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public CommonResult updateURL(@PathVariable(name = "id") Integer id) {
        return success(imageMapper.selectById(id));
    }

    @GetMapping("/generate-presigned-url")
    @Operation(summary = "oss获取分享链接")
    public CommonResult<String> generatePresignedUrl(String fileKey){
        return success(oosService.generatePresignedUrl(fileKey));
    }

}
